package com.yz.lzq.liwushuo.ui.modules.profile;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.decoration.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/12.
 */

public class ProfileFragment extends Fragment {
    private Context mContext = null;
    private XRecyclerView recyvleView_home;
    private Banner banner_pic;
    List<String> mStringList = new ArrayList<>();

    /**
     * 初始化Homefragment对象的单例模式
     *
     * @return 当前fragment对象
     */
    public static ProfileFragment getInstance() {
        ProfileFragment fragment = new ProfileFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    /**
     * @param inflater           布局加载器
     * @param container          父容器
     * @param savedInstanceState 保存状态的信息
     * @return 返回加载布局
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_profile, null);
    }

    /**
     * 初始化控件(黄油刀替代了)
     *
     * @param view               加载过得布局
     * @param savedInstanceState 保存状态的信息
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyvleView_home = (XRecyclerView) view.findViewById(R.id.recyvleView_home);

    }

    /**
     * 为控件赋值
     *
     * @param savedInstanceState 获取保存状态的信息
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
     /*   View view = LayoutInflater.from(mContext).inflate(R.layout.midle_main_view, null);
        initBanner(view);*/
        loadNetWork();

        GridLayoutManager manager = new GridLayoutManager(mContext, 2);
        recyvleView_home.setLayoutManager(manager);
        recyvleView_home.addItemDecoration(new SpacesItemDecoration(10));

        recyvleView_home.setHasFixedSize(true);
//        recyvleView_home.addHeaderView(view);
        loadGoodsNetWork();
    }

    /**
     * 加载商品的数据源
     */
    public void loadGoodsNetWork() {

    }

    /**
     * @param view 初始化Banner
     */
    private void initBanner(final View view) {
       /* Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlUtils.HOME_BANNER_PIC)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RtoInterface rtoInterface = retrofit.create(RtoInterface.class);
        Call<BannerPicBean> homeBannerPicBean = rtoInterface.getHomeBannerPicBean();
        homeBannerPicBean.enqueue(new Callback<BannerPicBean>() {
            @Override
            public void onResponse(Call<BannerPicBean> call, Response<BannerPicBean> response) {
                if (response.isSuccess()) {
                    mAdListBeanList = response.body().getAdList();
                    banner_pic = (Banner) view.findViewById(R.id.banner_pic);
                    for (int i = 0; i < mAdListBeanList.size(); i++) {
                        //得到Top_stories()内部类中的所有图片网址集合
                        mStringList.add(mAdListBeanList.get(i).getImgUrl());
                    }
                    //设置轮播模式
                    banner_pic.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                    //就这么些
                    banner_pic.setImageLoader(new GlideImageLoader());
                    //设置图片集合
                    banner_pic.setImages(mStringList);
                    //设置自动轮播，默认为true
                    banner_pic.isAutoPlay(true);
                    //设置轮播时间
                    banner_pic.setDelayTime(1500);
                    //设置指示器位置（当banner模式中有指示器时）
                    banner_pic.setIndicatorGravity(BannerConfig.RIGHT);
                    //banner设置方法全部调用完毕时最后调用
                    banner_pic.start();
                }
            }

            @Override
            public void onFailure(Call<BannerPicBean> call, Throwable t) {
            }
        });
*/
    }


    /**
     * 视图销毁
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 碎片销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 加载首页更新的数据
     */
    private void loadNetWork() {

 /*       Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlUtils.HOME_UPDATE)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Log.i("tag", "---->>>>>>>switchNetWork: 2");
        RtoInterface rtoInterface = retrofit.create(RtoInterface.class);
        Call<List<HomeUpdateBean>> homeUpdateBean = rtoInterface.getHomeUpdateBean();
        Log.i(TAG, "-----》switchNetWork:4");

        homeUpdateBean.enqueue(new Callback<List<HomeUpdateBean>>() {
            @Override
            public void onResponse(Call<List<HomeUpdateBean>> call, Response<List<HomeUpdateBean>> response) {
                Log.i("tag", "---->>>>>>>switchNetWork: 5" + response.isSuccess());
                if (response.isSuccess()) {
                }
            }

            @Override
            public void onFailure(Call<List<HomeUpdateBean>> call, Throwable t) {
                Log.i("tag", "---->>>>>>>switchNetWork: 6");
                t.printStackTrace();
            }
        });*/


    }
}
